package ar.com.huddleton.arquitectura;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Spinner;
import ar.com.huddleton.arquitectura.R;

public class UserPreferences extends Activity {

	private final String NOTIF_KEY = "notificaciones";
	private final String PAGO_KEY = "pagoInminente";
	private final String SONIDO_POS_KEY = "sonidoPosDefault";
	private final String SONIDO_KEY = "sonidoDefault";

	SharedPreferences preferences;
	SharedPreferences.Editor editor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_preferences);
		preferences = getPreferences(Context.MODE_PRIVATE);
		editor = preferences.edit();
		
		//chkNotificaciones Definitions
		CheckBox chkNotificaciones = (CheckBox) findViewById(R.id.chkNotificaciones);
		chkNotificaciones.setChecked(preferences.getBoolean(NOTIF_KEY,false));

		//chkPago Definitions
		CheckBox chkPagoInminente = (CheckBox) findViewById(R.id.chkPago);
		chkPagoInminente.setChecked(preferences.getBoolean(PAGO_KEY, false));
		
		//Spinner Sonidos
		final Spinner spinSonidos = (Spinner) findViewById(R.id.sonidos);
		spinSonidos.setSelection(preferences.getInt(SONIDO_POS_KEY, 0));
		
		// CheckBox Notificaciones--------------------------------------------------
		chkNotificaciones
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						editor.putBoolean(NOTIF_KEY, isChecked);
						editor.commit();
					}
				});
		// --------------------------------------------------------------------------
		
		//CheckBox PagoInminente-----------------------------------------------------
		chkPagoInminente
			.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					editor.putBoolean(PAGO_KEY, isChecked);
					editor.commit();
				}
			});
		//----------------------------------------------------------------------------
		
		//Spinner Sonidos
		spinSonidos.post(new Runnable() {
			
			public void run() {
				spinSonidos.setOnItemSelectedListener(new OnItemSelectedListener() {

					public void onItemSelected(AdapterView<?> parent, View view, int pos,
							long id) {
						
						MediaPlayer player = null;
						switch (pos) {
						case 1:
							player = MediaPlayer.create(getApplicationContext(), R.raw.anydo_pop);
							editor.putInt(SONIDO_KEY, R.raw.anydo_pop);
							editor.putInt(SONIDO_POS_KEY, pos);
							editor.commit();
							break;
						case 2:
							player = MediaPlayer.create(getApplicationContext(), R.raw.facebook_ringtone_pop);
							editor.putInt(SONIDO_KEY, R.raw.facebook_ringtone_pop);
							editor.putInt(SONIDO_POS_KEY, pos);
							editor.commit();
							break;
							
						default:
							break;
						}
						
						player.start();
						
					}

					public void onNothingSelected(AdapterView<?> arg0) {
//						// TODO Auto-generated method stub
//						
     				}
				});
			}
		});
		
	}
}
